package com.yalemang.framework.utils

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import java.lang.ref.WeakReference


class PermissionUtils(activity: Activity) {
    private var requestCode = 0
    private lateinit var permissionList: List<String>
    private val weakReferenceActivity = WeakReference(activity)
    private val weakReferenceContext = WeakReference(activity)
    private lateinit var permissionCallback: (Boolean)->Unit

    /**
     *
     * @param permissionString
     * @return false：具有该权限
     */
    @SuppressLint("ObsoleteSdkInt")
    private fun checkPermission(permissionString: String): Boolean {
        var flag = false
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(
                    weakReferenceContext.get()!!,
                    permissionString
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                flag = true
            }
        } else {
            flag = false
        }
        return flag
    }

    @SuppressLint("ObsoleteSdkInt")
    fun checkPermissions(
        permissionArray: Array<String>,
        permissionCallback: (Boolean)->Unit,
        requestCode: Int = 0
    ) {
        this.permissionList = listOf(*permissionArray)
        this.requestCode = requestCode
        this.permissionCallback = permissionCallback
        var flag = false
        for (permissionString in permissionList) {
            flag = checkPermission(permissionString)
            if (flag) {
                break
            }
        }

        //申请权限
        if (flag) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                weakReferenceActivity.get()!!.requestPermissions(permissionArray, requestCode)
            }
        } else {
            this.permissionCallback.invoke(true)
        }
    }

    @SuppressLint("ObsoleteSdkInt")
    fun checkPermissions(
        permissionList: List<String>,
        permissionCallback: (Boolean)->Unit,
        requestCode: Int = 0
    ) {
        this.permissionList = permissionList
        this.requestCode = requestCode
        this.permissionCallback = permissionCallback
        var flag = false
        for (permissionString in permissionList) {
            flag = checkPermission(permissionString)
            if (flag) {
                break
            }
        }

        //申请权限
        if (flag) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                weakReferenceActivity.get()!!.requestPermissions(permissionList.toTypedArray(), requestCode)
            }
        } else {
            this.permissionCallback.invoke(true)
        }
    }

    fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (this.requestCode == requestCode) {
            var sum = 0
            for (i in permissions.indices) {
                if (grantResults[i] == 0) {
                    sum++
                }
            }
            if (sum == permissionList.size) {
                //成功
                permissionCallback.invoke(true)
            } else {
                //失败
                permissionCallback.invoke(false)
            }
        }
    }

    //检测文件读写权限
    fun startCheckFileReadWritePermission(
        requestCode: Int = 0,
        permissionCallback: (Boolean)->Unit
    ) {
        val permissionList: MutableList<String> = ArrayList()
        permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        permissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        checkPermissions(permissionList, permissionCallback, requestCode)
    }


    interface PermissionCallback {
        fun requestResult(isSuccess: Boolean)
    }
}